﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace BlueLotus7.ExchangeCore
{
    public class SPDBDistributor : BaseDistributor
    {
        protected const string sourceUrl = "http://www.spdb.com.cn/showHL/SearchHV.aspx";
        protected const string tableEndTag = "</table>";

        public SPDBDistributor()
            : base(sourceUrl, 30, "SPDB")
        {
            this.borderTime = new TimeSpan(12, 0, 0);
        }

        protected override List<ExchangeItem> ConvertObjects(string content, DateTime date)
        {
            List<ExchangeItem> exchanges = new List<ExchangeItem>();

            if (!string.IsNullOrWhiteSpace(content))
            {
                int start = content.IndexOf("lblDate");
                if (start < 0)
                {
                    return exchanges;
                }
                start = content.IndexOf("<table", start);

                int end = content.IndexOf(tableEndTag, start);
                if (end < start)
                {
                    return exchanges;
                }

                content = content.Substring(start, end - start + tableEndTag.Length);
                content = content.Replace(" NOWRAP", string.Empty);
                content = content.Replace(" align=center height=25 class=12 style='font-size:11px'", string.Empty);
                content = content.Replace("<br>", string.Empty);
                content = content.Replace("bgcolor=#ffffff", string.Empty);
                content = content.Replace("bgcolor=#eff7fe", string.Empty);

                XDocument xDocument = XDocument.Parse(content);
                foreach (var row in xDocument.Root.Elements())
                {
                    if (row.Attribute("align") == null)
                    {
                        ExchangeItem item = ConvertExchangeItem(row, date);
                        if (item != null)
                        {
                            exchanges.Add(item);
                        }
                    }
                }
            }

            return exchanges;
        }

        protected override Dictionary<string, string> GenerateParameters(DateTime date)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();
            result.Add("txtpage", date.ToString("YYYY-MM-dd"));
            return result;
        }

        protected static ExchangeItem ConvertExchangeItem(XElement row, DateTime date)
        {
            try
            {
                ExchangeItem item = new ExchangeItem { Date = date.ToString("YYYY-MM-dd") };
                int index = 0;
                foreach (var one in row.Elements())
                {
                    string text = one.Value.Trim();

                    switch (index)
                    {
                        //英镑 GBP
                        case 0: item.CurrencyISO = text.Split(new char[] { ' ' })[1];
                            break;
                        case 1:
                            item.BaseUnit = Convert.ToInt32(text);
                            break;
                        case 2:
                            item.BankBuyingPrice = Convert.ToDecimal(text);
                            break;
                        case 3:
                            item.BankSellingPrice = Convert.ToDecimal(text);
                            break;
                        case 4:
                            item.BankNotesBuyingPrice = Convert.ToDecimal(text);
                            break;
                        case 5:
                            item.BankNotesSellingPrice = Convert.ToDecimal(text);
                            break;
                        case 6:
                            item.BankBenchmarkPrice = Convert.ToDecimal(text);
                            break;
                        default:
                            break;
                    }

                    index++;
                }

                return item;
            }
            catch { return null; }
        }
    }
}
